

function init()
{
	
	$.getJSON('/GoGoGadget/RemoveBusinessServlet', getBusinessInfo);

}

function getBusinessInfo(data)
{
	var list = document.getElementById("list");
	var business = data.business;
	var telephone = data.telephone;
	var cell = data.cell;
	var city = data.city;
	var email = data.email;
	var fax = data.fax;
	var postal = data.postal;
	var province = data.province;
	
	var listElement = document.createElement("li");
	var tele = document.createElement("li");
	var cellList = document.createElement("li");
	var cityList = document.createElement("li");
	var emailList = document.createElement("li");
	var faxList = document.createElement("li");
	var postalList = document.createElement("li");
	var provinceList = document.createElement("li");
	
	listElement.appendChild(document.createTextNode(business));
	listElement.setAttribute("business", business);
	list.appendChild(listElement);
	
	tele.appendChild(document.createTextNode(telephone));
	tele.setAttribute("telephone", telephone);
	list.appendChild(tele);
	
	cellList.appendChild(document.createTextNode(cell));
	cellList.setAttribute("cell", cell);
	list.appendChild(cellList);
	
	cityList.appendChild(document.createTextNode(city));
	cityList.setAttribute("city", city);
	list.appendChild(cityList);
	
	emailList.appendChild(document.createTextNode(email));
	emailList.setAttribute("email", email);
	list.appendChild(emailList);
	
	faxList.appendChild(document.createTextNode(fax));
	faxList.setAttribute("fax", fax);
	list.appendChild(faxList);
	
	postalList.appendChild(document.createTextNode(postal));
	postalList.setAttribute("postal", postal);
	list.appendChild(postalList);
	
	provinceList.appendChild(document.createTextNode(province));
	provinceList.setAttribute("province", province);
	list.appendChild(provinceList);
}

function initEdit()
{
	
	$.getJSON('/GoGoGadget/EditBusinessServlet', test);

}

function test(data) {
	var list = document.getElementById("list");
	var business = data.business;
	var telephone = data.telephone;
	var cell = data.cell;
	var city = data.city;
	var email = data.email;
	var fax = data.fax;
	var address = data.address;
	var postal = data.postal;
	var province = data.province;
	
	document.getElementById("business").value = business;
	document.getElementById("telephone").value = telephone;
	document.getElementById("email").value = email;
	document.getElementById("fax").value = fax;
	document.getElementById("cell").value = cell;
	document.getElementById("city").value = city;
	document.getElementById("address").value = address;
	document.getElementById("postal").value = postal;
	document.getElementById("province").value = province;
	
	var listElement = document.createElement("li");
	var tele = document.createElement("li");
	var cellList = document.createElement("li");
	var cityList = document.createElement("li");
	var emailList = document.createElement("li");
	var faxList = document.createElement("li");
	var postalList = document.createElement("li");
	var provinceList = document.createElement("li");
	
	//business = document.getElementById("business");
	//city = document.getElementById("city");
	
	listElement.appendChild(document.createTextNode(business));
	listElement.setAttribute("business", business);
	list.appendChild(listElement);
	
	tele.appendChild(document.createTextNode(telephone));
	tele.setAttribute("telephone", telephone);
	list.appendChild(tele);
	
	cellList.appendChild(document.createTextNode(cell));
	cellList.setAttribute("cell", cell);
	list.appendChild(cellList);
	
	cityList.appendChild(document.createTextNode(city));
	cityList.setAttribute("city", city);
	list.appendChild(cityList);
	
	emailList.appendChild(document.createTextNode(email));
	emailList.setAttribute("email", email);
	list.appendChild(emailList);
	
	faxList.appendChild(document.createTextNode(fax));
	faxList.setAttribute("fax", fax);
	list.appendChild(faxList);
	
	postalList.appendChild(document.createTextNode(postal));
	postalList.setAttribute("postal", postal);
	list.appendChild(postalList);
	
	provinceList.appendChild(document.createTextNode(province));
	provinceList.setAttribute("province", province);
	list.appendChild(provinceList);
	
}

function initializeEmployee()
{
	alert("here");
	//var idNumber = document.getElementById("idNumber").getAttribute("value");
	//$.getJSON('/GoGoGadget/UpdateEmployeeServlet?idNumber=' + idNumber, getEmployeeInfo);
	$.getJSON('/GoGoGadget/UpdateEmployeeServlet', getEmployeeInfo);
}

function getEmployeeInfo(data){

	alert(idNumber);
	var firstName = data.employeeFirstName;
	var lastName = data.employeeLastName;

	var employer = data.employer;
	var telephone = data.telephone;
	var cell = data.cellPhone;
	
	var wage = data.wage;
	var SIN = data.SIN;
	
	var address = data.address;
	var city = data.city;
	var province = data.province;
	var postal = data.postal;
	
	//var form = document.getElementById("updateForm");
	document.getElementById("firstName").value = firstName;
	document.getElementById("lastName").value = lastName;
	document.getElementById("employer").value = employer;
	document.getElementById("telephone").value = telephone;
	document.getElementById("cell").value = cell;
	document.getElementById("wage").value = wage;
	document.getElementById("SIN").value = SIN;
	document.getElementById("address").value = address;
	document.getElementById("city").value = city;
	document.getElementById("province").value = province;
	document.getElementById("postal").value = postal;
	
	//alert(firstName);

	
}


/*
function employeeProfile()
{
	//alert("hi5");
	$.getJSON('/GoGoGadget/EmployeeServlet', getEmployeeProfile);
	
}
*/

function getEmployeeProfile(data)
{
	
	var list = document.getElementById("list");
	var firstName = data.employeeFirstName;
	var lastName = data.employeeLastName;
	var business = data.employer;
	var telephone = data.telephone;
	var cell = data.cellPhone;
	var wage = data.wage;
	var address = data.address;
	var province = data.province;
	var postal = data.postal;
	//alert("hi5");
	
	var listElement = document.createElement("li");
	var tele = document.createElement("li");
	var cellList = document.createElement("li");
	var cityList = document.createElement("li");
	var emailList = document.createElement("li");
	var faxList = document.createElement("li");
	var postalList = document.createElement("li");
	var provinceList = document.createElement("li");
	
	listElement.appendChild(document.createTextNode(firstName));
	listElement.setAttribute("firstName", firstName);
	list.appendChild(listElement);
	
	lastList.appendChild(document.createTextNode(lastName));
	lastList.setAttribute("lastName", lastName);
	list.appendChild(lastList);
	
	lastList.appendChild(document.createTextNode(lastName));
	lastList.setAttribute("lastName", lastName);
	list.appendChild(lastList);
						
}


function getEmployees()
{
	//alert("bork");
	$.getJSON('/GoGoGadget/ListAllEmployees', displayEmployees);
	
}

function displayEmployees(data)
{
	
	var index;
	var employees = data.employees;
	var list = document.getElementById("list");
	
	for (index=0; index<employees.length; index++) {
		var listElement = document.createElement("li");
		var employee = employees[index];
		
		listElement.appendChild(document.createTextNode(employee.name));
		
		listElement.setAttribute("employeeid", employee.employeeid);
		listElement.setAttribute("employee", employee.name);
		
		//alert(employee);
		listElement.onclick = employeeProfile;
	
		
		list.appendChild(listElement);
		//alert(employee);
	}
	
}

function empProfile()
{
	
	var urlParser = new ArgumentURL();
	var id = urlParser.getArgument("employee");
	alert("asdf");
	$.getJSON('/GoGoGadget/EmployeeServlet?employee=' + id, getEmployeeProfile);
	
	/*var first = urlParser.getArgument("first");
	var last = urlParser.getArgument("last");
	var employer = urlParser.getArgument("employer");
	alert("here");
	listElement.appendChild(document.createTextNode(firstName));
	listElement.setAttribute("firstName", firstName);
	list.appendChild(listElement);
	
	tele.appendChild(document.createTextNode(lastName));
	tele.setAttribute("lastName", lastName);
	list.appendChild(tele);*/
}

function employeeProfile()
{
	var employee = this.getAttribute("employeeid");
	
	document.location = "Employee.jsp?employee=" + employee;
	//alert(employee);
	//$.getJSON('/GoGoGadget/EmployeeServlet?' + 'employee=' + employee, getEmployeeProfile);
	//$.getJSON('/GoGoGadget/EmployeeServlet?' + 'employee=' + employee, getEmployeeProfile);
}

function getEmployeeProfile(data)
{
	alert("here");
	var list = document.getElementById("list");
	var first = data.employeeFirstName;
	var last = data.employeeLastName;
	var employer = data.employer;
	var telephone = data.telephone;
	var cellphone = data.cellPhone;
	var address = data.address;
	var city = data.city;
	var province = data.province;
	var postal = data.postalCode;
	var dateHired = data.dateHired;
	
	var listElement = document.createElement("li");
	var firstList = document.createElement("li");
	var lastList = document.createElement("li");
	var employerList = document.createElement("li");
	var telephoneList = document.createElement("li");
	var cellphoneList = document.createElement("li");
	var addressList = document.createElement("li");
	var cityList = document.createElement("li");
	var provinceList = document.createElement("li");
	var postalList = document.createElement("li");
	var dateHiredList = document.createElement("li");
	
	listElement.appendChild(document.createTextNode(first));
	listElement.setAttribute("first", first);
	list.appendChild(listElement);
	
	lastList.appendChild(document.createTextNode(last));
	lastList.setAttribute("last", last);
	list.appendChild(lastList);
	
	employerList.appendChild(document.createTextNode(employer));
	employerList.setAttribute("employer", employer);
	list.appendChild(employerList);
	
	telephoneList.appendChild(document.createTextNode(telephone));
	telephoneList.setAttribute("telephone", telephone);
	list.appendChild(telephoneList);
	
	cellphoneList.appendChild(document.createTextNode(telephone));
	cellphoneList.setAttribute("cellphone", cellphone);
	list.appendChild(cellphoneList);
	
	addressList.appendChild(document.createTextNode(address));
	addressList.setAttribute("address", address);
	list.appendChild(addressList);
	
	cityList.appendChild(document.createTextNode(city));
	cityList.setAttribute("city", city);
	list.appendChild(cityList);
	
	provinceList.appendChild(document.createTextNode(province));
	provinceList.setAttribute("province", province);
	list.appendChild(provinceList);
	
	postalList.appendChild(document.createTextNode(postal));
	postalList.setAttribute("postal", postal);
	list.appendChild(postalList);
	
	document.getElementById("empID").value = id;
	//dateHiredList.appendChild(document.createTextNode(dateHired));
	//dateHiredList.setAttribute("dateHired", dateHired);
	//list.appendChild(dateHired);
}

function removeEmployee(){
	
	var urlParser = new ArgumentURL();
	var id = urlParser.getArgument("employee");
	
	$.getJSON('/GoGoGadget/EmployeeServlet?employee=' + id, deleteEmployeeProfile);
	
}

function deleteEmployeeProfile(data){
	alert("hi");
}

function listInfo(){
	//alert("food");
	var employee = this.getAttribute("employee");

	//alert(employee);
	$.getJSON('/GoGoGadget/EmployeeServlet?' + 'employee=' + employee, listEmployeeInfo);
	//$.getJSON('/GoGoGadget/EmployeeServlet', listEmployeeInfo);
}

function listEmployeeInfo(data){
	//alert("food");
	var list = document.getElementById("list");
	var firstName = data.employeeFirstName;
	var lastName = data.employeeLastName;

	
	var listElement = document.createElement("li");
	var firstName = document.createElement("li");

	listElement.appendChild(document.createTextNode(firstName));
	listElement.setAttribute("firstName", firstName);
	list.appendChild(listElement);
	
	tele.appendChild(document.createTextNode(lastName));
	tele.setAttribute("lastName", lastName);
	list.appendChild(lastName);
	
}



function setupEmployeeInfo()
{
	var url = new ArgumentURL();
	var arg;

	arg = url.getArgument("first");
	
	if (arg != undefined && arg != "undefined"){
		arg = arg + " " + url.getArgument("last");
		if (arg != undefined && arg != "undefined"){
			document.getElementById("name").appendChild(document.createTextNode(arg));
		}
		document.title = arg;
	}
	
	arg = url.getArgument("employer");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("employer").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("telephone");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("telephone").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("cellPhone");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("cellPhone").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("dateHired");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("dateHired").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("dateFired");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("dateFired").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("wage");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("wage").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("SIN");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("SIN").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("address");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("address").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("city");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("city").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("province");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("province").appendChild(document.createTextNode(arg));
	}
	
	arg = url.getArgument("employeeID");
	if (arg != undefined && arg != "undefined"){
		document.getElementById("employeeID").appendChild(document.createTextNode(arg));
		
	}
}


function getEmployeesForEdit()
{
	//alert("sadf");
	$.getJSON('/GoGoGadget/ListAllEmployees', displayEmployeesForEdit);
	//$.getJSON('/GoGoGadget/ListAllEmployees', displayEmployees);
}

function displayEmployeesForEdit(data)
{
	
	var index;
	var employees = data.employees;
	var list = document.getElementById("list");
	
	for (index=0; index<employees.length; index++) {
		var listElement = document.createElement("li");
		var employee = employees[index];
		
		listElement.appendChild(document.createTextNode(employee.name));
		
		listElement.setAttribute("employeeid", employee.employeeid);
		listElement.setAttribute("employee", employee.name);
		
		//alert(employee);
		listElement.onclick = employeeProfileForEdit;
	
		
		list.appendChild(listElement);
		//alert(employee);
	}
}

function employeeProfileForEdit()
{
	var employee = this.getAttribute("employeeid");
	
	document.location = "UpdateEmployee.jsp?employee=" + employee;
	
	
	//var employee = this.getAttribute("employee");
	//$.getJSON('/GoGoGadget/EmployeeServlet?' + 'employee=' + employee, getEmployeeProfileForEdit);
	
	//alert(employee);
	//$.getJSON('/GoGoGadget/EmployeeServlet?' + 'employee=' + employee, getEmployeeProfile);
}

function updateEmpProfile()
{
	alert("food");
	var urlParser = new ArgumentURL();
	var id = urlParser.getArgument("employee");
	
	$.getJSON('/GoGoGadget/UpdateEmployeeServlet?employee=' + id, getEmployeeProfileForEdit);

}

function getEmployeeProfileForEdit(data)
{
	var list = document.getElementById("list");
	var first = data.employeeFirstName;
	var last = data.employeeLastName;
	var employer = data.employer;

	var listElement = document.createElement("li");
	var firstList = document.createElement("li");
	var lastList = document.createElement("li");
	var employerList = document.createElement("li");

	document.getElementById("first")value = first;
	//listElement.appendChild(document.createTextNode(first));
	//listElement.setAttribute("first", first);
	//list.appendChild(listElement);
	
	//lastList.appendChild(document.createTextNode(last));
	//lastList.setAttribute("last", last);
	//list.appendChild(lastList);
	
	//employerList.appendChild(document.createTextNode(employer));
	//employerList.setAttribute("employer", employer);
	//list.appendChild(employerList);
	
	
}

function ArgumentURL() {
	this.getArgument = _getArg;
	this.setArgument = _setArg;
	this.removeArgument = _removeArg;
	this.toString    = _toString;	//Allows the object to be printed
					//no need to write toString()
	this.arguments   = new Array();

	// Initiation
	var separator = "&";
	var equalsign = "=";
	
	var str = window.location.search.replace(/%20/g, " ");
	var index = str.indexOf("?");
	var sInfo;
	var infoArray = new Array();

	var tmp;
	
	if (index != -1) {
		sInfo = str.substring(index+1,str.length);
		infoArray = sInfo.split(separator);
	}

	for (var i=0; i<infoArray.length; i++) {
		tmp = infoArray[i].split(equalsign);
		if (tmp[0] != "") {
			var t = tmp[0];
			this.arguments[tmp[0]] = new Object();
			this.arguments[tmp[0]].value = tmp[1];
			this.arguments[tmp[0]].name = tmp[0];
		}
	}
	

	
	function _toString() {
		var s = "";
		var once = true;
		for (i in this.arguments) {
			if (once) {
				s += "?";
				once = false;
			}
			s += this.arguments[i].name;
			s += equalsign;
			s += this.arguments[i].value;
			s += separator;
		}
		return s.replace(/ /g, "%20");
	}
	
	function _getArg(name) {
		if(this.arguments[name] == undefined) {
			return null;
		}
		
		if (typeof(this.arguments[name].name) != "string") {
			return null;
		}
		else {
			return this.arguments[name].value;
		}
	}
	
	function _setArg(name,value) {
		this.arguments[name] = new Object();
		this.arguments[name].name = name;
		this.arguments[name].value = value;
	}
	
	function _removeArg(name) {
		this.arguments[name] = null;
	}
	
	return this;
}
